{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n",
        "# Optimizing Vision Transformer Model for Deployment\n",
        "\n",
        "[Jeff Tang](https://github.com/jeffxtang),\n",
        "[Geeta Chauhan](https://github.com/gchauhan/)\n",
        "\n",
        "Vision Transformer models apply the cutting-edge attention-based\n",
        "transformer models, introduced in Natural Language Processing to achieve\n",
        "all kinds of the state of the art (SOTA) results, to Computer Vision\n",
        "tasks. Facebook Data-efficient Image Transformers [DeiT](https://ai.facebook.com/blog/data-efficient-image-transformers-a-promising-new-technique-for-image-classification)\n",
        "is a Vision Transformer model trained on ImageNet for image\n",
        "classification.\n",
        "\n",
        "In this tutorial, we will first cover what DeiT is and how to use it,\n",
        "then go through the complete steps of scripting, quantizing, optimizing,\n",
        "and using the model in iOS and Android apps. We will also compare the\n",
        "performance of quantized, optimized and non-quantized, non-optimized\n",
        "models, and show the benefits of applying quantization and optimization\n",
        "to the model along the steps.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## What is DeiT\n",
        "\n",
        "Convolutional Neural Networks (CNNs) have been the main models for image\n",
        "classification since deep learning took off in 2012, but CNNs typically\n",
        "require hundreds of millions of images for training to achieve the\n",
        "SOTAresults. DeiT is a vision transformer model that requires a lot less\n",
        "data and computing resources for training to compete with the leading\n",
        "CNNs in performing image classification, which is made possible by two\n",
        "key components of of DeiT:\n",
        "\n",
        "-  Data augmentation that simulates training on a much larger dataset;\n",
        "-  Native distillation that allows the transformer network to learn from\n",
        "   a CNN’s output.\n",
        "\n",
        "DeiT shows that Transformers can be successfully applied to computer\n",
        "vision tasks, with limited access to data and resources. For more\n",
        "details on DeiT, see the [repo](https://github.com/facebookresearch/deit)\n",
        "and [paper](https://arxiv.org/abs/2012.12877).\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Classifying Images with DeiT\n",
        "\n",
        "Follow the README at the DeiT repo for detailed information on how to\n",
        "classify images using DeiT, or for a quick test, first install the\n",
        "required packages:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# pip install torch torchvision timm pandas requests"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To run in Google Colab, uncomment the following line:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# !pip install timm pandas requests"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "then run the script below:\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from PIL import Image\n",
        "import torch\n",
        "import timm\n",
        "import requests\n",
        "import torchvision.transforms as transforms\n",
        "from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD\n",
        "\n",
        "print(torch.__version__)\n",
        "# should be 1.8.0\n",
        "\n",
        "\n",
        "model = torch.hub.load('facebookresearch/deit:main', 'deit_base_patch16_224', pretrained=True)\n",
        "model.eval()\n",
        "\n",
        "transform = transforms.Compose([\n",
        "    transforms.Resize(256, interpolation=3),\n",
        "    transforms.CenterCrop(224),\n",
        "    transforms.ToTensor(),\n",
        "    transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD),\n",
        "])\n",
        "\n",
        "img = Image.open(requests.get(\"https://raw.githubusercontent.com/pytorch/ios-demo-app/master/HelloWorld/HelloWorld/HelloWorld/image.png\", stream=True).raw)\n",
        "img = transform(img)[None,]\n",
        "out = model(img)\n",
        "clsidx = torch.argmax(out)\n",
        "print(clsidx.item())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The output should be 269, which, according to the ImageNet list of class\n",
        "index to [labels file](https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a), maps to ‘timber\n",
        "wolf, grey wolf, gray wolf, Canis lupus’.\n",
        "\n",
        "Now that we have verified that we can use the DeiT model to classify\n",
        "images, let’s see how to modify the model so it can run on iOS and\n",
        "Android apps.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Scripting DeiT\n",
        "To use the model on mobile, we first need to script the\n",
        "model. See the [Script and Optimize recipe](https://pytorch.org/tutorials/recipes/script_optimized.html) for a\n",
        "quick overview. Run the code below to convert the DeiT model used in the\n",
        "previous step to the TorchScript format that can run on mobile.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model = torch.hub.load('facebookresearch/deit:main', 'deit_base_patch16_224', pretrained=True)\n",
        "model.eval()\n",
        "scripted_model = torch.jit.script(model)\n",
        "scripted_model.save(\"fbdeit_scripted.pt\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The scripted model file fbdeit_scripted.pt of size about 346MB is\n",
        "generated.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Quantizing DeiT\n",
        "To reduce the trained model size significantly while\n",
        "keeping the inference accuracy about the same, quantization can be\n",
        "applied to the model. Thanks to the transformer model used in DeiT, we\n",
        "can easily apply dynamic-quantization to the model, because dynamic\n",
        "quantization works best for LSTM and transformer models (see [here](https://pytorch.org/docs/stable/quantization.html?highlight=quantization#dynamic-quantization)\n",
        "for more details).\n",
        "\n",
        "Now run the code below:\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Use 'fbgemm' for server inference and 'qnnpack' for mobile inference\n",
        "backend = \"fbgemm\" # replaced with qnnpack causing much worse inference speed for quantized model on this notebook\n",
        "model.qconfig = torch.quantization.get_default_qconfig(backend)\n",
        "torch.backends.quantized.engine = backend\n",
        "\n",
        "quantized_model = torch.quantization.quantize_dynamic(model, qconfig_spec={torch.nn.Linear}, dtype=torch.qint8)\n",
        "scripted_quantized_model = torch.jit.script(quantized_model)\n",
        "scripted_quantized_model.save(\"fbdeit_scripted_quantized.pt\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This generates the scripted and quantized version of the model\n",
        "fbdeit_quantized_scripted.pt, with size about 89MB, a 74% reduction of\n",
        "the non-quantized model size of 346MB!\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can use the ``scripted_quantized_model`` to generate the same\n",
        "inference result:\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "out = scripted_quantized_model(img)\n",
        "clsidx = torch.argmax(out)\n",
        "print(clsidx.item())\n",
        "# The same output 269 should be printed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Optimizing DeiT\n",
        "The final step before using the quantized and scripted\n",
        "model on mobile is to optimize it:\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from torch.utils.mobile_optimizer import optimize_for_mobile\n",
        "optimized_scripted_quantized_model = optimize_for_mobile(scripted_quantized_model)\n",
        "optimized_scripted_quantized_model.save(\"fbdeit_optimized_scripted_quantized.pt\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The generated fbdeit_optimized_scripted_quantized.pt file has about the\n",
        "same size as the quantized, scripted, but non-optimized model. The\n",
        "inference result remains the same.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "out = optimized_scripted_quantized_model(img)\n",
        "clsidx = torch.argmax(out)\n",
        "print(clsidx.item())\n",
        "# Again, the same output 269 should be printed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Using Lite Interpreter\n",
        "\n",
        "To see how much model size reduction and inference speed up the Lite\n",
        "Interpreter can result in, let’s create the lite version of the model.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "optimized_scripted_quantized_model._save_for_lite_interpreter(\"fbdeit_optimized_scripted_quantized_lite.ptl\")\n",
        "ptl = torch.jit.load(\"fbdeit_optimized_scripted_quantized_lite.ptl\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Although the lite model size is comparable to the non-lite version, when\n",
        "running the lite version on mobile, the inference speed up is expected.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Comparing Inference Speed\n",
        "\n",
        "To see how the inference speed differs for the four models - the\n",
        "original model, the scripted model, the quantized-and-scripted model,\n",
        "the optimized-quantized-and-scripted model - run the code below:\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "with torch.autograd.profiler.profile(use_cuda=False) as prof1:\n",
        "    out = model(img)\n",
        "with torch.autograd.profiler.profile(use_cuda=False) as prof2:\n",
        "    out = scripted_model(img)\n",
        "with torch.autograd.profiler.profile(use_cuda=False) as prof3:\n",
        "    out = scripted_quantized_model(img)\n",
        "with torch.autograd.profiler.profile(use_cuda=False) as prof4:\n",
        "    out = optimized_scripted_quantized_model(img)\n",
        "with torch.autograd.profiler.profile(use_cuda=False) as prof5:\n",
        "    out = ptl(img)\n",
        "\n",
        "print(\"original model: {:.2f}ms\".format(prof1.self_cpu_time_total/1000))\n",
        "print(\"scripted model: {:.2f}ms\".format(prof2.self_cpu_time_total/1000))\n",
        "print(\"scripted & quantized model: {:.2f}ms\".format(prof3.self_cpu_time_total/1000))\n",
        "print(\"scripted & quantized & optimized model: {:.2f}ms\".format(prof4.self_cpu_time_total/1000))\n",
        "print(\"lite model: {:.2f}ms\".format(prof5.self_cpu_time_total/1000))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The results running on a Google Colab are:\n",
        "\n",
        "::\n",
        "\n",
        "   original model: 1236.69ms\n",
        "   scripted model: 1226.72ms\n",
        "   scripted & quantized model: 593.19ms\n",
        "   scripted & quantized & optimized model: 598.01ms\n",
        "   lite model: 600.72ms\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following results summarize the inference time taken by each model\n",
        "and the percentage reduction of each model relative to the original\n",
        "model.\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "\n",
        "df = pd.DataFrame({'Model': ['original model','scripted model', 'scripted & quantized model', 'scripted & quantized & optimized model', 'lite model']})\n",
        "df = pd.concat([df, pd.DataFrame([\n",
        "    [\"{:.2f}ms\".format(prof1.self_cpu_time_total/1000), \"0%\"],\n",
        "    [\"{:.2f}ms\".format(prof2.self_cpu_time_total/1000),\n",
        "     \"{:.2f}%\".format((prof1.self_cpu_time_total-prof2.self_cpu_time_total)/prof1.self_cpu_time_total*100)],\n",
        "    [\"{:.2f}ms\".format(prof3.self_cpu_time_total/1000),\n",
        "     \"{:.2f}%\".format((prof1.self_cpu_time_total-prof3.self_cpu_time_total)/prof1.self_cpu_time_total*100)],\n",
        "    [\"{:.2f}ms\".format(prof4.self_cpu_time_total/1000),\n",
        "     \"{:.2f}%\".format((prof1.self_cpu_time_total-prof4.self_cpu_time_total)/prof1.self_cpu_time_total*100)],\n",
        "    [\"{:.2f}ms\".format(prof5.self_cpu_time_total/1000),\n",
        "     \"{:.2f}%\".format((prof1.self_cpu_time_total-prof5.self_cpu_time_total)/prof1.self_cpu_time_total*100)]],\n",
        "    columns=['Inference Time', 'Reduction'])], axis=1)\n",
        "\n",
        "print(df)\n",
        "\n",
        "\"\"\"\n",
        "        Model                             Inference Time    Reduction\n",
        "0\toriginal model                             1236.69ms           0%\n",
        "1\tscripted model                             1226.72ms        0.81%\n",
        "2\tscripted & quantized model                  593.19ms       52.03%\n",
        "3\tscripted & quantized & optimized model      598.01ms       51.64%\n",
        "4\tlite model                                  600.72ms       51.43%\n",
        "\"\"\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### Learn More\n",
        "\n",
        "- [Facebook Data-efficient Image Transformers](https://ai.facebook.com/blog/data-efficient-image-transformers-a-promising-new-technique-for-image-classification)_\n",
        "- [Vision Transformer with ImageNet and MNIST on iOS](https://github.com/pytorch/ios-demo-app/tree/master/ViT4MNIST)_\n",
        "- [Vision Transformer with ImageNet and MNIST on Android](https://github.com/pytorch/android-demo-app/tree/master/ViT4MNIST)_\n",
        "\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)]"
    },
    "vscode": {
      "interpreter": {
        "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
